/**
 * 
 */
package org.codesketch.controller.web;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;

import org.codesketch.controller.configuration.ApplicationContextConfig;
import org.codesketch.controller.configuration.MongoDBTemplateConfiguration;
import org.codesketch.controller.configuration.WebMvcConfiguration;
import org.codesketch.controller.configuration.XMLMarshalerConfiguration;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

/**
 * @author Quirno Brizi
 *
 */
public class WebContextConfiguration implements WebApplicationInitializer {

	/* (non-Javadoc)
	 * @see org.springframework.web.WebApplicationInitializer#onStartup(javax.servlet.ServletContext)
	 */
	@Override
	public void onStartup(ServletContext sc)
			throws ServletException {
		AnnotationConfigWebApplicationContext ac = new AnnotationConfigWebApplicationContext();
		ac.register(new Class[] {ApplicationContextConfig.class, MongoDBTemplateConfiguration.class, XMLMarshalerConfiguration.class});
		sc.addListener(new ContextLoaderListener(ac));
		
		AnnotationConfigWebApplicationContext webCtx = new AnnotationConfigWebApplicationContext();
		webCtx.register(WebMvcConfiguration.class);
		javax.servlet.ServletRegistration.Dynamic dynamic = sc.addServlet("dispatcher", new DispatcherServlet(webCtx));
		dynamic.setLoadOnStartup(1);
		dynamic.addMapping("/");
	}

}
